package com.auth.control.auth;


import com.auth.control.model.UserRoleDto;
import com.auth.domain.module.vo.authmanage.RoleVO;
import com.auth.domain.service.IUserRoleService;
import com.redis.sdk.cachecore.CacheLocal;
import com.redis.sdk.cachecore.CacheLocalUpdate;
import com.redis.sdk.cachecore.CacheRedis;
import com.redis.sdk.cachecore.CacheRedisUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 用户角色表(用户绑定角色) 前端控制器
 * </p>
 *
 * @author cyj
 * @since 2020-10-21
 */
@RestController
@RequestMapping("/user/role/{userId}")
@Slf4j
@Api(tags = "用户绑定角色服务")
public class UserRoleController {

    @Autowired
    private IUserRoleService userRoleService;

    @GetMapping("/all")
    @ApiOperation("查询用户所有的角色")
    @CacheRedis("#userId")
    @CacheLocal("#userId")
    public List<RoleVO> getUserRoles(@PathVariable Long userId) {
        List<RoleVO> list = userRoleService.getUserRoles(userId);
        return list;
    }

    @PostMapping
    @ApiOperation("保存用户关联的所有角色")
    @CacheRedisUpdate("#userId")
    @CacheLocalUpdate("#userId")
    public List<RoleVO> saveUserRoles(@PathVariable Long userId,
                                          @RequestBody List<UserRoleDto> userRoleDtos) {
        userRoleService.saveUserRoles(userId, userRoleDtos);
        return userRoleService.getUserRoles(userId);
    }
}

